package com.bigpang.framework.redis.baseopt.set;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

/**
 * @Author YongQiang
 * @Date 2021/6/8 16:41
 * @Version 1.0
 */
public class SetDemo {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("10.130.132.137", 6379);

        //1	SADD key member1 [member2] 向集合添加一个或多个成员
        jedis.sadd("setkey1", "value1", "value2");
        //2	SCARD key 获取集合的成员数
        System.out.println("setkey1的长度是:" + jedis.scard("setkey1"));
        //3	SDIFF key1 [key2] 返回给定所有集合的差集
        Set<String> sdiff = jedis.sdiff("setkey1", "setkey2");
        //4	SDIFFSTORE destination key1 [key2] 返回给定所有集合的差集并存储在 destination 中
        Long sdiffstore = jedis.sdiffstore("destKey", "setkey1", "setkey2");
        //5	SINTER key1 [key2] 返回给定所有集合的交集
        Set<String> sinter = jedis.sinter("setkey1", "setkey2");
        //6	SINTERSTORE destination key1 [key2] 返回给定所有集合的交集并存储在 destination 中
        Long sinterstore = jedis.sinterstore("destKey", "setkey1", "setkey2");
        //7	SISMEMBER key member 判断 member 元素是否是集合 key 的成员
        Boolean sismember = jedis.sismember("setkey1", "value1");
        //8	SMEMBERS key 返回集合中的所有成员
        Set<String> setkey1 = jedis.smembers("setkey1");
        //9	SMOVE source destination member 将 member 元素从 source 集合移动到 destination 集合
        jedis.smove("setkey1", "setkey2", "value1");
        //10	SPOP key 移除并返回集合中的一个随机元素
        jedis.spop("setkey1");
        //11	SRANDMEMBER key [count] 返回集合中一个或多个随机数
        String setkey11 = jedis.srandmember("setkey1");
        List<String> setkey12 = jedis.srandmember("setkey1", 2);
        //12	SREM key member1 [member2] 移除集合中一个或多个成员
        Long srem = jedis.srem("setkey1", "value2");
        //13	SUNION key1 [key2] 返回所有给定集合的并集
        Set<String> sunion = jedis.sunion("setkey1", "setkey2");
        //14	SUNIONSTORE destination key1 [key2] 所有给定集合的并集存储在 destination 集合中
        jedis.sunionstore("destkey","setkey1", "setkey2");
        //15	SSCAN key cursor [MATCH pattern] [COUNT count] 迭代集合中的元素

        jedis.close();
    }
}
